package com.app.jzgdirector.base.activity;


import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.jzgdirector.R;
import com.app.jzgdirector.common.view.ProgressBarDialog;
import com.common.baselibrary.ApplicationManager;
import com.common.baselibrary.utils.StringUtils;

import butterknife.OnClick;


public abstract class BaseToolBarActivity extends AppCompatActivity {
    private static final String TAG = "BaseToolBarActivity";

    public LinearLayout root;
    public TextView mLeftTitle;
    public TextView mCenterTitle;
    public TextView mRightTitle;
    public Toolbar mToolbar;



    private ProgressBarDialog myProgressBarDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        super.setContentView(R.layout.activity_tool_bar_base);
        mLeftTitle = (TextView) findViewById(R.id.left_title);
        mCenterTitle = (TextView) findViewById(R.id.center_title);
        mRightTitle = (TextView) findViewById(R.id.right_title);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        root = (LinearLayout) findViewById(R.id.root);
    }

    @Override
    public void setContentView(int layoutId) {
        setContentView(View.inflate(this, layoutId, null));
    }

    private void initToolbar() {
        if (mToolbar != null) {
            //将Toolbar显示到界面
            mToolbar.setTitle("");//不显示toolbar标题
            mToolbar.setContentInsetStartWithNavigation(0);//返回图标距离标题为0
            setSupportActionBar(mToolbar);
        }
    }

    @Override
    public void setContentView(View view) {
        root.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        initToolbar();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (null != mToolbar) {//不用toolbar自带的返回按钮,用自定义的
            //mToolbar.setNavigationIcon(R.mipmap.actionbar_back_pressed);//后退按钮图片
            //mToolbar.setNavigationOnClickListener(v -> onBackPressed());
        }
    }

    @OnClick(R.id.left_title)
    public void back() {
        onBackPressed();
    }

    public void setLeftTitle(String leftTitle) {
        mLeftTitle.setVisibility(View.VISIBLE);
        if (StringUtils.isNotEmpty(leftTitle)) {
            mLeftTitle.setText(leftTitle);
        }
    }

    public void setLeftBtn(int contentRes, int drawableRes) {
        mLeftTitle.setVisibility(View.VISIBLE);
        mLeftTitle.setText(contentRes);
        Drawable drawable = getResources().getDrawable(drawableRes);
        if (drawable != null) {
            drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                    drawable.getMinimumHeight());
            mLeftTitle.setCompoundDrawables(drawable, null, null, null);
        }
    }

    public void setCenterTitle(String centerTitle) {
        mCenterTitle.setVisibility(View.VISIBLE);
        mCenterTitle.setText(centerTitle);
    }

    public void setRightTitle(String rightTitle) {
        mRightTitle.setVisibility(View.VISIBLE);
        mRightTitle.setText(rightTitle);
    }

    public void setRightTitle(String rightTitle, int drawableRes) {
        mRightTitle.setVisibility(View.VISIBLE);
        mRightTitle.setText(rightTitle);
        Drawable drawable = getResources().getDrawable(drawableRes);
        if (drawable != null) {
            drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                    drawable.getMinimumHeight());
            mRightTitle.setCompoundDrawables(drawable, null, null, null);
        }
    }

    public Toolbar getmToolbar() {
        return mToolbar;
    }


    /**
     * 显示等待进度条
     */
    public void showLoadingProgressBar() {
        try {
            showLoadingProgressBar(null, false, "");
        } catch (Exception e) {
        }
    }

    /**
     * 显示等待进度条
     *
     * @param needShowWithBg 是否需要显示背景
     * @param loadingHint    显示背景时，可设置文本提示内容
     */
    public void showLoadingProgressBar(final ProgressBarDialog.OnDialogCancelListener
                                               onDialogCancelListener, final boolean needShowWithBg, final String loadingHint) {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            ApplicationManager.getMainHandler().post(new Runnable() {
                @Override
                public void run() {
                    showLoadingProgressBar(onDialogCancelListener, needShowWithBg, loadingHint);
                }
            });
            return;
        }

        if (myProgressBarDialog == null) {
            myProgressBarDialog = new ProgressBarDialog(this);
            myProgressBarDialog.setOnCancelListener(onDialogCancelListener);
        }
        if (!myProgressBarDialog.isShowing()) {
            myProgressBarDialog.show(needShowWithBg, loadingHint);
        }
    }

    /**
     * 取消菊花样式的进度条显示
     */
    public void cancelLoadingProgressBar() {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            ApplicationManager.getMainHandler().post(new Runnable() {
                @Override
                public void run() {
                    cancelLoadingProgressBar();
                }
            });
            return;
        }
        if (null != myProgressBarDialog && myProgressBarDialog.isShowing()) {
            myProgressBarDialog.manualCancel();
        }
    }

}
